//
//  WQCMyPlanViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/21.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCMyPlanViewController.h"
#import "configuration.h"
#import "WQCPlanTableViewCell.h"
#import "WQCPlanDetailedViewController.h"
@interface WQCMyPlanViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *planTableview;
@property(nonatomic,strong)NSMutableArray  *bankidArray;
@property(nonatomic,strong)NSMutableArray  *lastnumberArray;
@property(nonatomic,strong)NSMutableArray  *amountArray;
@property(nonatomic,strong)NSMutableArray  *puangeArray;
@property(nonatomic,strong)NSMutableArray  *createTimeArr;
@property(nonatomic,strong)NSMutableArray  *totalConsumeNumArr;
@property(nonatomic,strong)NSMutableArray  *totalRepaymentNumArr;
@property(nonatomic,strong)NSMutableArray  *stateArr;
@property(nonatomic,strong)NSMutableArray  *planidArr;
@property(nonatomic,strong)NSMutableArray  *cardidArr;
@property(nonatomic,strong)NSMutableArray  *balanceArr;


@property(nonatomic,strong)NSString  *planid;

@property(nonatomic,strong)NSString  *cardid;


@end

@implementation WQCMyPlanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _bankidArray = [[NSMutableArray alloc]init];
    _amountArray = [[NSMutableArray alloc]init];
    _totalConsumeNumArr = [[NSMutableArray alloc]init];
    _totalRepaymentNumArr = [[NSMutableArray alloc]init];
    _lastnumberArray = [[NSMutableArray alloc]init];
    _puangeArray = [[NSMutableArray alloc]init];
    _createTimeArr = [[NSMutableArray alloc]init];
    _stateArr = [[NSMutableArray alloc]init];
    _planidArr = [[NSMutableArray alloc]init];
    _cardidArr = [[NSMutableArray alloc]init];
    _balanceArr = [[NSMutableArray alloc]init];

    self.navigationController.navigationBarHidden =NO;
    [self loadmyplan];
    self.view.backgroundColor = WYCGlobalBg;
    [self setupNav];
    self.planTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, kScreenW, kScreenH) style:UITableViewStyleGrouped];
    self.planTableview.dataSource = self;
    self.planTableview.delegate = self;
    self.planTableview.backgroundColor = [UIColor clearColor];
    self.planTableview.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.planTableview];
}
- (void)setupNav {
    
    [self.navigationController.navigationBar setBarTintColor:wycMain];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.title = @"我的计划";
}
#pragma mark - TableView DataSource Method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.bankidArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 215;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString * strID = @"cell";
    
    WQCPlanTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strID];
    
    if (cell == nil)
    {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"WQCPlanTableViewCell" owner:self options:nil]lastObject];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.lastNumber.text =[NSString stringWithFormat:@"%@(%@)",[WYCAccountTool unarchiveuserName],[_lastnumberArray objectAtIndex:indexPath.row]];
   cell.dateLb.text =[self.createTimeArr objectAtIndex:indexPath.row];
    
   cell.puangeLb.text =[NSString stringWithFormat:@"%@天%@期",self.totalConsumeNumArr[indexPath.row],self.totalRepaymentNumArr[indexPath.row]];
  
    cell.amountLb.text =  [NSString stringWithFormat:@"%.1f",[self.amountArray[indexPath.row]doubleValue] ];;
    cell.typeLb.text = self.planidArr[indexPath.row];
    cell.stateLb.text =self.cardidArr[indexPath.row];
  cell.blanceLb.text =  [NSString stringWithFormat:@"%.1f",[self.balanceArr[indexPath.row] doubleValue]];;
    cell.numLb.text =self.puangeArray[indexPath.row];
    cell.typeLb.text =@"实际还款金额";
//    _planid = cell.typeLb.text;
//    _cardid = cell.stateLb.text;
    cell.bgBig.frame =CGRectMake(5, 10, ScreenWidth-10, 210);
    cell.bankSmall.layer.borderWidth = 10 * px;
    cell.bankSmall.layer.cornerRadius = 50 * px;
    cell.bankSmall.layer.borderColor = [[UIColor whiteColor] CGColor];
   NSString  *bankId = self.bankidArray [indexPath.row];
    NSInteger bankid = [bankId integerValue];
    switch (bankid) {
        case 10000://招商银行
            cell.bankNm.text=@"招商银行";
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_zs"];
            break;
        case 10001://工商银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_gs"];
            cell.bankNm.text =@"工商银行";
            
            break;
        case 10002://农业银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_ny"];
            cell.bankNm.text =@"农业银行";
            
            break;
        case 10003://  建设银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_js"];
            cell.bankNm.text =@"建设银行";
            
            break;
        case 10004://中国银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_zg"];
            cell.bankNm.text =@"中国银行";
            
            break;
        case 10005:  //浦发银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_pf"];
            cell.bankNm.text =@"浦发银行";
            
            break;
        case 10006:  //交通银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_jt"];
            cell.bankNm.text =@"交通银行";
            
            break;
        case 10007:  //民生银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_ms"];
            cell.bankNm.text =@"民生银行";
            
            break;
        case 10009:  //广东发展银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_gf"];
            cell.bankNm.text =@"广东银行";
            
            break;
        case 10010:  //中信银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_zx"];
            cell.bankNm.text =@"中信银行";
            
            break;
        case 10011:  //  华夏银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_hx"];
            cell.bankNm.text =@"华夏银行";
            
            break;
        case 10012:  //  兴业银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_xy"];
            cell.bankNm.text =@"兴业银行";
            
            break;
        case 10014:  //  江苏银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_gs"];
            cell.bankNm.text =@"江苏银行";
            
            break;
        case 10016:  //  北京银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_bj"];
            cell.bankNm.text =@"北京银行";
            
            break;
        case 10020://10020  光大银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_gd"];
            cell.bankNm.text =@"光大银行";
            
            break;
        case  10024://平安银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_pa"];
            cell.bankNm.text =@"平安银行";
            
            break;
        case 10027:  //上海银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_sh"];
            cell.bankNm.text =@"上海银行";
            
            break;
        case  10028://  中国邮政储蓄银行
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_yz"];
            cell.bankNm.text =@"邮政银行";
            
            break;
            
        default:
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_gs"];
            
            break;
    }
    
    NSString *state=self.stateArr [indexPath.row];
    NSLog(@"信用卡状态%@",state);
    if ([state isEqualToString:@"1"]) {
        cell.stateLb.text =@"待执行";
    }
    else if ([state isEqualToString:@"2"])
    {
        cell.stateLb.text =@"执行中";

    }
    else if ([state isEqualToString:@"3"])
    {
        cell.stateLb.text =@"执行成功";

    }
    else if ([state isEqualToString:@"4"])
    {
        cell.stateLb.text =@"执行失败";

    }
    else if ([state isEqualToString:@"5"])

    {
        cell.stateLb.text =@"还款终止";

    }
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WQCPlanDetailedViewController  *detailView = [[WQCPlanDetailedViewController alloc]init];
    detailView.planid =self.planidArr[indexPath.row];
    detailView.cardid = self.cardidArr[indexPath.row];
    [self.navigationController pushViewController:detailView animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)loadmyplan
{
    [HttpTool POST:getUserPlan params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"我的计划%@",responseObject);
        
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                NSArray * resultarray = responseObject[@"result"];
                
                if (![resultarray isKindOfClass:[NSNull class]])
                {
                for (NSDictionary * dic in resultarray) {

                    NSString   *bankid = dic[@"bankId"];
                    [_bankidArray addObject:bankid];
                    NSString   *lastNumber = dic[@"lastNumber"];
                    [_lastnumberArray addObject:lastNumber];
                    NSDictionary  *plan = dic[@"plan"];
                    [_amountArray addObject:[NSString stringWithFormat:@"%@", plan[@"amount"]]];
                    NSString  * createTime = plan[@"createTime"];
                    [_createTimeArr addObject:createTime];
                    [_totalConsumeNumArr addObject:[NSString stringWithFormat:@"%@", plan[@"totalConsumeNum"]]];
                    [_totalRepaymentNumArr addObject:[NSString stringWithFormat:@"%@", plan[@"totalRepaymentNum"]]];
                    [_puangeArray addObject:[NSString stringWithFormat:@"%@", plan[@"totalCharge"]]];
                    [_planidArr addObject:[NSString stringWithFormat:@"%@",plan[@"planId"]]];
                    [_cardidArr addObject:[NSString stringWithFormat:@"%@",plan[@"cardId"]]];
                    [_balanceArr addObject:[NSString stringWithFormat:@"%@",plan[@"balance"]]];
                    [_stateArr addObject:[NSString stringWithFormat:@"%@",plan[@"state"]]];
                    }
        }
                
                if (self.amountArray.count == 0) {
                    [self addView];
                }
            }
        [self.planTableview reloadData];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
- (void)addView{
    UILabel * showlabel = [[UILabel alloc] init];
    showlabel.text = @"暂无还款计划";
    showlabel.textAlignment = NSTextAlignmentCenter;
    showlabel.frame = CGRectMake(0, 350, ScreenWidth, 60* px);
    showlabel.textColor = [UIColor lightGrayColor];
    showlabel.font = [UIFont systemFontOfSize:SmallFont];
    [self.view addSubview:showlabel];
    
}

@end
